<?php

ignore_user_abort(true); 		// 后台运行，不受前端断开连接影响
set_time_limit(0); 				// 脚本将永久运行
chdir(dirname(__FILE__));
// error_reporting(E_ERROR);
require_once ("../../config.php"); // 引入配置
require_once ("../../log.php");
require_once ("../api_tool.php");
require_once ("core.php");

// LOGINFO($_GET, '=== $_GET');
// 获取商品数据
if ($_GET['shop_code'] == '1') {
	$shop_data = simQuery("SELECT * FROM item_ordinary WHERE item_id = {$_GET['shop_id']} AND channel_id = {$_GET['channel_id']};");		// 获取普通库
} else {
	$shop_data = simQuery("SELECT * FROM item_preferred WHERE item_id = {$_GET['shop_id']} AND channel_id = {$_GET['channel_id']};");		// 获取优选库
}

// LOGINFO($shop_data, '=== $shop_data');

if (empty($shop_data)) {
	echo json_encode(array(
		'state' => -1,
		'msg'	=> '不是当前数据库查出的商品!',
	));
	exit();
}


$wx_data = simQuery("SELECT *, wx.id AS wx_id, wx.taobao_ance AS taobao_ance, wx.pdd_ance AS pdd_ance, wx.jd_ance AS jd_ance FROM wechat_group AS wx, robot AS bot WHERE wx.id = {$_GET['wx_id']} AND wx.robot_id = bot.id");
if (empty($wx_data)) {
	echo json_encode(array (
		'state' => -2,
		'msg'	=> '不存在当前微信群!',
	));
	exit();
}

// 获取所有的链接
if ($shop_data['coupon_url'] != '') {
	$cou_list = explode(',', $shop_data['coupon_url']);
	$amo_list = explode(',', $shop_data['coupon_amount']);
	$fee_list = explode(',', $shop_data['coupon_start_fee']);
}

$shop_fee_price = array();

// 获取短链接
if (count($cou_list) > 0) {
	for ($i = 0; $i < count($cou_list); ++$i) {
		$resp = getJdunionOpenPromotionBysubunionid($shop_data['item_id'], $wx_data['jd_ance'], $cou_list[$i]);
		if (!empty($resp['data']['shortURL'])) {
			$surl = $resp['data']['shortURL'];
			array_push($shop_fee_price, array(
				'all_price' => $fee_list[$i],
				'price'		=> $amo_list[$i],
				'shortURL'  => $resp['data']['shortURL'],
			));
		}
	}

	if (empty($surl)) {
		if ($_GET['shop_code'] == '2') {
			simQuery("DELETE FROM item_preferred WHERE id = {$_GET['shop_id']};");
		}

		echo json_encode(array (
			'state' => -4,
			'msg'	=> '没有当前短链接!',
		));
		exit();
	}
}

// 获取最优的优惠券
$discount = 0;
$shortURL = $shop_data['url'];
if (count($shop_fee_price) > 0) {
	for ($i = 0; $i < count($shop_fee_price); ++$i) {
		$e = $shop_fee_price[$i];
		if ($e['price'] > $discount && $e['all_price'] < $shop_data['coupon_url']) {
			$discount = $e['price'];
			$shortURL = $e['shortURL'];
		}
	}
}
$data = array();
foreach($shop_data as $key => $value) {
	if (!is_int($key) && !is_array($value) && !empty($value)) {
		$data[$key] = $value;
	}
}

foreach ($wx_data as $key => $value) {
	if (!is_int($key) && !is_array($value) && !empty($value)) {
		$data[$key] = $value;
	}
}

// 商品记录选取一下组合成数据发送
$data['good_img'] = $_GET['good_img'];
$data['discount'] = $discount;
$data['shortURL'] = $shortURL;

$host = "127.0.0.1";
$port = 80;
$url = "/api/api_jd/send.php";

simQuery("UPDATE wechat_group SET wait_time = 0 WHERE id = {$wx_data['wx_id']}");
sendFZSHttp($host, $port, $url, $data);

echo json_encode(array(
	'state' => 1,
	'msg'	=> '成功!',
));
exit();

?>